package org.dromara.application.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.application.domain.bo.AppUserBo;
import org.dromara.application.domain.vo.AppUserVo;
import org.dromara.application.domain.AppUser;
import org.dromara.application.mapper.AppUserMapper;
import org.dromara.application.service.IAppUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 应用和用户关联Service业务层处理
 *
 * @author YCJZ
 * @date 2025-09-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppUserServiceImpl implements IAppUserService {

    private final AppUserMapper baseMapper;

    /**
     * 查询应用和用户关联
     *
     * @param clientId 主键
     * @return 应用和用户关联
     */
    @Override
    public AppUserVo queryById(String clientId){
        return baseMapper.selectVoById(clientId);
    }

    /**
     * 根据条件查询应用和用户关联
     *
     * @param clientId  应用id
     * @param loginId   用户id
     * @return 应用和用户关联
     */
    @Override
    public AppUserVo getByClientIdAndLoginId(String clientId, Long loginId) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<AppUser>()
            .eq(AppUser::getClientId, clientId)
            .eq(AppUser::getUserId, loginId)
            .eq(AppUser::getStatus, "0"));
    }

    /**
     * 分页查询应用和用户关联列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 应用和用户关联分页列表
     */
    @Override
    public TableDataInfo<AppUserVo> queryPageList(AppUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUser> lqw = buildQueryWrapper(bo);
        Page<AppUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的应用和用户关联列表
     *
     * @param bo 查询条件
     * @return 应用和用户关联列表
     */
    @Override
    public List<AppUserVo> queryList(AppUserBo bo) {
        LambdaQueryWrapper<AppUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUser> buildQueryWrapper(AppUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUser> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppUser::getClientId);
        lqw.orderByAsc(AppUser::getUserId);
        lqw.orderByAsc(AppUser::getOpenId);
        return lqw;
    }

    /**
     * 新增应用和用户关联
     *
     * @param bo 应用和用户关联
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppUserBo bo) {
        AppUser add = MapstructUtils.convert(bo, AppUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setClientId(add.getClientId());
        }
        return flag;
    }

    /**
     * 修改应用和用户关联
     *
     * @param bo 应用和用户关联
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppUserBo bo) {
        AppUser update = MapstructUtils.convert(bo, AppUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除应用和用户关联信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
